package com.coding.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.coding.bean.Menu;
import com.coding.bean.ShowNotice;
import com.coding.bean.User;
import com.coding.config.properties.VsProperties;
import com.coding.service.MenuService;
import com.coding.service.NoticeService;
import com.coding.utils.Userinfo;

@Controller
public class HomeController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private VsProperties vsProperties;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public String home(Model model) {
        log.info("home page");
        if (!SecurityUtils.getSubject().isAuthenticated() || Userinfo.getUser() == null) {
			model.addAttribute("kaptcha", vsProperties.isKptchaswich());
            return "login2";
        }

        //右侧的通知
        List<ShowNotice> list = noticeService.getAllShowNotice();
		model.addAttribute("noticelist", list);

        List<Menu> menuList = menuService.getUserMenu();
		model.addAttribute("menulist", menuList);

		model.addAttribute("username", Userinfo.getUsername());
		model.addAttribute("tip", ((User) SecurityUtils.getSubject().getPrincipal()).getRoleTip());
		model.addAttribute("sex", Userinfo.getSex());
        return "index1";
    }

}
